package cn.com.agree.bigdata.vmdatahandle.utils;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.SchedulerException;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @description: 带有job信息的定时器管理器
 * @author: LiYuan
 * @version: 1.0
 * @create: 2019-04-24
 **/
@Data
@Slf4j
public class QuartzManagerWithJob {
    private static QuartzManager quartzManager;
    private static QuartzJobInfo quartzJobInfo = new QuartzJobInfo();

    public QuartzManagerWithJob() throws SchedulerException {
        init();
    }

    public void init() throws SchedulerException {
        if (quartzManager == null) {
            quartzManager = new QuartzManager();
        }
    }

    /**
     * 添加job，并存储job信息
     *
     * @param jobName
     * @param jobGroupName
     * @param triggerName
     * @param triggerGroupName
     * @param jobClass
     * @param cron
     * @param scheduleJobName
     * @param scheduleJob
     */
    private void addJob(String jobName,
                        String jobGroupName,
                        String triggerName,
                        String triggerGroupName,
                        Class<? extends Job> jobClass,
                        String cron,
                        String scheduleJobName,
                        Object scheduleJob) {
        log.info("enter addJob:");

        String uuid = UUID.randomUUID().toString();
        if (jobName.isEmpty()) {
            jobName = "job-" + uuid;
        }

        if (triggerName.isEmpty()) {
            triggerName = "trigger-" + uuid;
        }

        if (jobGroupName.isEmpty()) {
            jobGroupName = "group-" + uuid;
        }

        if (triggerGroupName.isEmpty()) {
            triggerGroupName = "group-" + uuid;
        }
        String cronExpression = "0 " + cron + " ?";
        quartzManager.addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cronExpression, scheduleJobName, scheduleJob);

        String[] jobInfo = {jobName, triggerName, cron, jobGroupName, triggerGroupName};
        quartzJobInfo.add(jobInfo);

        log.info("after addJob, quartzJobInfo:{}", JSON.toJSONString(quartzJobInfo));


    }

    /**
     * 运行定时器入口
     */
    public void run() {
        quartzManager.startJobs();
    }

    public void addNewJobForCluster(String jobName,
                                    String jobGroupName,
                                    String triggerName,
                                    String triggerGroupName,
                                    Class<? extends Job> jobClass,
                                    String[] cronList,
                                    String scheduleJobName,
                                    Object scheduleJob) {
        log.info("");
        log.info("");
        log.info("------------------------------");
        log.info("create jobs:");

        for (String cron : cronList) {
            addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron, scheduleJobName, scheduleJob);
            log.info("");
        }
    }

    /**
     * 存储job类
     */
    @Data
    private static class QuartzJobInfo {
        private List<String[]> jobList = new ArrayList<>(3);

        void add(String[] arr) {
            jobList.add(arr);
        }
    }

}


